let mongoose = require('./index');
let { Schema, Model } = mongoose;

// 维修订单数据模型
const OrderSchema = new Schema({
  // 订单基本信息
  orderNumber: {
    type: String,
    required: true,
    unique: true,
    default: function() {
      return 'WX' + Date.now() + Math.floor(Math.random() * 1000).toString().padStart(3, '0');
    }
  },
  
  // 客户信息
  customerInfo: {
    name: {
      type: String,
      required: true,
      trim: true
    },
    phone: {
      type: String,
      required: true,
      trim: true
    },
    address: {
      type: String,
      trim: true
    }
  },
  
  // 设备信息
  deviceInfo: {
    type: {
      type: String,
      required: true,
      trim: true
    },
    problem: {
      type: String,
      required: true,
      trim: true
    }
  },
  
  // 订单状态
  status: {
    type: String,
    enum: ['pending', 'confirmed', 'in_progress', 'completed', 'cancelled'],
    //状态依次为待确认、已确认、维修中、已完成、已取消
    default: 'pending'
  },
  
  // 时间信息
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  },
  
  // 预约时间
  appointmentTime: {
    type: Date
  },
  
  // 维修费用
  cost: {
    type: Number,
    default: 0
  },
  
  // 备注
  notes: {
    type: String,
    trim: true
  },
  
  // 支付状态
  paid: {
    type: Boolean,
    default: false
  }
}, {
  timestamps: true // 自动管理createdAt和updatedAt
});

// 创建订单模型
const Order = mongoose.model('Order', OrderSchema);

module.exports = {
  Order
};
